package Thread;

/**
 * @author liyang
 * @version 1.0
 * @package Thread
 * @file DaemonThreadDemo.java
 * @description
 * @time 2024-04-07 18:44
 */
public class DaemonThreadDemo {
    public static void main(String[] args) {
        //守护线程作用：跟着死（进程或者说别的线程都结束它也就被强制结束了）
        Thread rose = new Thread(){
            @Override
            public void run(){
                for(int i=0;i<5;i++){
                    System.out.println("rose:let me go!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("rose:啊啊啊啊啊啊AAAAAaaaaa...");
                System.out.println("噗通");
            }
        };

        Thread jack = new Thread(){
            @Override
            public void run(){
                while(true){
                    System.out.println("jack:you jump!i jump!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        rose.start();
        //设置守护线程必须在线程启动前进行!
        jack.setDaemon(true);
        jack.start();
        System.out.println("主线程结束了");
//        while(true);//若主线程不结束，进程不会结束，那么jack也不会被强制杀死
    }
}
